// pages/active/active.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    layer: true,
    ruleLayer: false,
    appLayer: false,
    showAppHot: false,
    hotDetails: false,
    signin:false,
    issign: false,
    isactive: false,
    invitationdouble:false,
    wxinvitation:false,
    officialsharehb: false,
    friendsHbDetails:false,
    myFriends: {},
    myNewFriends: {},
    countDown: "00:00:00",
    user:'',
    officialhb:'',
    sharetype:'',
    testTitile:"44444"
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getMyfriend()
  },
  layer: function() { //规则弹窗
    this.setData({
      layer: true,
      ruleLayer: true,
    })
  },
  closeLayer: function() { // 关闭所有弹窗
    this.setData({
      layer: false,
      ruleLayer: false,
      appLayer: false,
      hotDetails: false
    })
  },
  bubble: function() { //阻止冒泡
    console.log("阻止冒泡")
  },

  // 点击官方红包页面
  openRedMoney: function() { // 打开中青看点红包
    this.setData({
      appLayer: true,
      layer: true
    })
  },
  openHotDetails: function() { // 领取官方红包 点击开图片
    this.setData({
      hotDetails: true,
      appLayer: false,
    })
  },
  openMyHotDetails: function(e) { // 打开我的好友红包
    this.setData({
      friendsHbDetails:true,
      friendhbdetails: app.globalData.friendsInfo,
      f: e.currentTarget.dataset.index
    })
    this.getMyFriendsHot(app.globalData.friendsInfo[e.currentTarget.dataset.index].friend)
  },

  closefriendsHbDetails: function () { // 关闭我的好友红包
    this.setData({
      friendsHbDetails: false
    })
    this.onLoad()
  },

  RedDouble: function (e){  //好友邀请红包翻倍 
    this.setData({
      invitationdouble: true,
      user : app.globalData.friendsInfo,
      a: e.currentTarget.dataset.index
    })
  },

  closeinvitationdouble: function () { //关闭好友邀请红包翻倍 
    this.setData({
      invitationdouble: false,
    })
  },

  invitation: function (e) {  //普通好友邀请红包
    this.setData({
      wxinvitation: true,
      wxfriends: app.globalData.friendsInfo,
      i: e.currentTarget.dataset.index
    })
  },

  closewxinvitation: function () { //关闭普通好友邀请红包 
    this.setData({
      wxinvitation: false,
    })
    this.onLoad()
  },

  closeofficialsharehb:function(){ //关闭官方红包详情弹窗
    this.setData({
      officialsharehb: false,
    })
    this.onLoad()
  },

  getMyfriend: function() { // 获取我的好友
    let that = this;
    delete app.globalData.systemInfo.code
    delete app.globalData.systemInfo.encryptedData
    delete app.globalData.systemInfo.iv
    delete app.globalData.systemInfo.sign
    var dataObj = app.globalData.systemInfo;
    dataObj.uid = app.globalData.userInfo.uid
    dataObj.channel = "6666";
    dataObj.sign = app.makeSign(dataObj)
    wx.request({
      url: 'https://kandian.youth.cn:8181/WechatApp/ShareSign/getFriends.json',
      data: dataObj,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res)
        app.globalData.friendsInfo = res.data.data.list
        that.setData({
          myFriends: res.data.data,
        }, function() {
          for (var i = 0; i < that.data.myFriends.list.length; i++) {
            that.data.myFriends.list[i].nowTime = "";
            that.myFcountDown(that.data.myFriends.list[i], i)
          }
        })
        //处理官方倒计时
        if (res.data.data.red.nextRed == 0) {
          that.setData({
            showAppHot: false
          })
        } else {
          that.setData({
            showAppHot: true
          })
          that.countDown(res.data.data.red.nextRed)
        }
      },
      fail: function(err) { //失败的方法
        console.log(err)
      }
    })
  },
  // 领取我的好友红包
  getMyFriendsHot: function(friend) {
    let that = this
    var friendslObj = app.globalData.systemInfo
    delete friendslObj.cookie
    delete friendslObj.cookie_id
    friendslObj.friend = friend
    friendslObj.sign = app.makeSign(friendslObj)
    wx.request({
      url: 'https://kandian.youth.cn:8181/WechatApp/ShareSign/getRed.json',
      data: friendslObj,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log('0.0', res)
        that.setData({
          friednscore: res.data.score
        })
      }
    })
  },

  //获取官方红包
  getofficialHot:function(){
    let that = this 
    var officialObj = app.globalData.systemInfo
    delete officialObj.cookie
    delete officialObj.cookie_id
    officialObj.sign = app.makeSign(officialObj)
    wx.request({
      url: 'https://kandian.youth.cn:8181/WechatApp/ShareSign/tored.json',
      data: officialObj,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log('ofo',res)
        app.globalData.officialHbDetails=res.data.data
        console.log('opo0.0', app.globalData.officialHbDetails)
      }
    })
  },
  countDown: function(timer) { //倒计时
    let that = this
    clearInterval(time)
    var time = setInterval(function() {
      timer--
      let hour = parseInt(timer / 3600)
      let fen = parseInt(timer / 60 % 60)
      let miao = parseInt(timer % 60)
      fen < 10 ? fen = '0' + fen : fen
      miao < 10 ? miao = '0' + miao : miao
      let timeText = hour + ":" + fen + ":" + miao
      that.setData({
        countDown: timeText
      })
    }, 1000)
  },
  myFcountDown: function(item, index) {
    let that = this
    clearInterval(time)
    if (item.time != 0){
      var time = setInterval(function () {
        item.time--
        let hour = parseInt(item.time / 3600)
        let fen = parseInt(item.time / 60 % 60)
        let miao = parseInt(item.time % 60)
        fen < 10 ? fen = '0' + fen : fen
        miao < 10 ? miao = '0' + miao : miao
        let timeText = hour + ":" + fen + ":" + miao
        that.setData({
          nowTime: timeText
        })
      }, 1000)
    }
    
  },
  hbDetails: function (hbtimer) { //官方红包详情倒计时
    let that = this
    clearInterval(time)
    var time = setInterval(function () {
      hbtimer--
      let hour = parseInt(hbtimer / 3600)
      let fen = parseInt(hbtimer / 60 % 60)
      let miao = parseInt(hbtimer % 60)
      fen < 10 ? fen = '0' + fen : fen
      miao < 10 ? miao = '0' + miao : miao
      let timeText = hour + ":" + fen + ":" + miao
      that.setData({
        hbDetails: timeText
      })
    }, 1000)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button' && res.target.id == 'officialshare') {
      this.setData({
        testTitile: app.globalData.userInfo.nickname+'送你一个现金红包，点击即可领取',
        sharetype : 1
      })
    }
    var that = this
    return {
      title: app.globalData.userInfo.nickname + '送你一个现金红包，点击即可领取',
      path: '/pages/news/news?uid=' + app.globalData.userInfo.uid,
      imageUrl: 'https://res.youth.cn/20180914/img/sharehb.png' ,
      success: function(e) {
        console.log(e)
        that.getofficialHot()
        if (that.data.sharetype == 1){
          that.setData({
            officialsharehb: true,
            officialhb: app.globalData.officialHbDetails
          })
          console.log(that.data.officialhb)
          console.log(app.globalData.officialHbDetails)
          that.hbDetails(that.data.officialhb.time)
        }
      }
    }
  }
})